/**
 * 
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.print.attribute.standard.RequestingUserName;

import entity.*;
import utils.DBHelper;

/**
 * @author Chen Lei
 *
 */
public class AdminDAO {
	private AdminDAO() {
		
	}

	public static AdminDAO getInstance() {
		return new AdminDAO();
	}
	/**
	 * 新增管理员
	 *
	 */
	public int addAdmin(Admin admin) {

		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "insert into admin values(null,?,?,?)";
			PreparedStatement ps = c.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
		
			ps.setString(1, admin.getAdmin_name());
			ps.setString(2, admin.getAdmin_password());
			ps.setString(3, admin.getAdmin_tel());
			
			ps.execute();
			
			ResultSet rs = ps.getGeneratedKeys();
			if (rs.next()) {
				int admin_id =  rs.getInt(1);
				return admin_id;
				//admin.setAdmin_id(admin_id);
			}

			DBHelper.closeConnection(c, ps, rs);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	/**
	 * 删除管理员
	 *
	 */
	public void delete(int admin_id) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			Statement s = c.createStatement();

			String sql = "delete from admin where admin_id = " + admin_id;

			s.execute(sql);

			DBHelper.closeConnection(c, s, null);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 更新管理员信息
	 *
	 */
	public void update(Admin admin) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "update admin set admin_name = ?, admin_password = ?,"
					+ "admin_tel = ? where admin_id = " + admin.getAdmin_id() ;
			
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setString(1, admin.getAdmin_name());
			ps.setString(2, admin.getAdmin_password());
			ps.setString(3, admin.getAdmin_tel());
			
			ps.execute();	

			DBHelper.closeConnection(c, ps, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	/**
	 * 通过id查询某个管理员
	 *
	 */
	public Admin get(int admin_id) {
		Admin ad = null;
		try {
			
			Connection c = DBHelper.getInstance().getConnection();

			Statement s = c.createStatement();

			String sql = "select * from admin where admin_id = " + admin_id;

			ResultSet rs = s.executeQuery(sql);

			while (rs.next()) {

				ad = new Admin();
				
				String admin_name = rs.getString("admin_name");
				String admin_password = rs.getString("admin_password");
				String admin_tel = rs.getString("admin_tel");
				
				ad.setAdmin_id(admin_id);
				ad.setAdmin_name(admin_name);
				ad.setAdmin_password(admin_password);
				ad.setAdmin_tel(admin_tel);
				
			}

			DBHelper.closeConnection(c, s, rs);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return ad;
	}
	/**
	 * 得到论坛所有管理员信息
	 *
	 */
	public List<Admin> list() {
		return list(0, Short.MAX_VALUE);
	}
	/**
	 * 得到论坛从start开始count个管理员信息
	 *
	 */
	public List<Admin> list(int start, int count) {
		List<Admin> admins = new ArrayList<Admin>();

		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "select * from admin ORDER BY admin_id DESC limit ?,? ";

			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, count);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				Admin ad = new Admin();
				int admin_id = rs.getInt("admin_id");
				String admin_name = rs.getString("admin_name");
				String admin_password = rs.getString("admin_password");
				String admin_tel = rs.getString("admin_tel");
				
				ad.setAdmin_id(admin_id);
				ad.setAdmin_name(admin_name);
				ad.setAdmin_password(admin_password);
				ad.setAdmin_tel(admin_tel);
				
				admins.add(ad);
			}
			DBHelper.closeConnection(c, ps, rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return admins;
	}
}
